<template>
    <CommonModal :isVisible="isVisible" :backgroundImage="backgroundImage" @close="closeModal" :modalStyle="modalStyle">
        <template #content>
            <div class="question-area flex flex-col items-center justify-center">
                <div class="flex items-end justify-center h-[200px] gap-4">
                    <img :src="`${apiBaseUrl}${currentQuestion.kmlfile}`" alt="" style="width: 100%; height: 200px"
                        v-if="currentQuestion.kmlfile && currentQuestion.types === '1'">
                    <!-- types 为 2 是视频，3 是音频 -->

                    <template v-if="isShowAnswer">
                        <video v-if="currentQuestion.types === '2' && currentQuestion.paths"
                            :src="`${apiBaseUrl}${currentQuestion.paths}`" controls preload
                            style="width: 100%; height: 200px"></video>
                    </template>
                    <template v-else>
                        <video v-if="currentQuestion.types === '2' && currentQuestion.kmlfile"
                            :src="`${apiBaseUrl}${currentQuestion.kmlfile}`" controls preload
                            style="width: 100%; height: 200px"></video>
                    </template>
                    <audio v-if="currentQuestion.types === '3' && currentQuestion.kmlfile"
                        :src="`${apiBaseUrl}${currentQuestion.kmlfile}`" controls preload style="width: 520px;"></audio>
                </div>

                <template v-if="isShowAnswer">
                    <div class="question-title">
                        答案：{{ currentQuestion.options[currentQuestion.answer] }}
                    </div>
                    <div class="question-title flex" style="margin-top: -20px;">
                        <div class="common-text">简介：</div>
                        <div class="flex-1 common-text">{{ currentQuestion.remark }}</div>
                    </div>
                </template>
                <template v-else>
                    <div class="question-title">{{ currentQuestion.question }}</div>

                    <!-- options 单选 -->
                    <div class="options-container">
                        <div class="options-row">
                            <div v-for="(optionText, optionKey) in currentQuestion.options" :key="optionKey"
                                class="option-item" :class="{ 'selected': selectedOption === optionKey }"
                                @click="selectOption(optionKey)">
                                <div class="option-checkbox"></div>
                                <div class="option-text">{{ optionText }}</div>
                            </div>
                        </div>
                    </div>
                </template>
            </div>
        </template>

        <template #buttons>
            <div class="number-btn" v-if="!isShowAnswer"
                :style="{ backgroundImage: `url(${questionImages[currentIndex + 1]})` }"></div>
            <div class="types-btn" v-if="!isShowAnswer"
                :style="{ backgroundImage: `url(${typesMap[currentQuestion.types]})` }"></div>
            
            <!-- 确认按钮 -->
            <div v-if="!isShowAnswer" class="confirm-btn" @click="handleConfirm"></div>
            <!-- 继续按钮 -->
            <div v-if="isShowAnswer" class="next-btn" @click="continueNextQuestion"></div>
        </template>
    </CommonModal>
</template>

<script setup>
import { ref, defineEmits, computed, reactive, watch } from 'vue'
import CommonModal from './CommonModal.vue'
import api from '@/api/index'
import modal4Png from '@/assets/images/4/modal.png'
import correctPng from '@/assets/images/4/correct.png'
import wrongPng from '@/assets/images/4/wrong.png'

import question1Png from '@/assets/images/4/q1.png'
import question2Png from '@/assets/images/4/q2.png'
import question3Png from '@/assets/images/4/q3.png'
import question4Png from '@/assets/images/4/q4.png'
import question5Png from '@/assets/images/4/q5.png'
import question6Png from '@/assets/images/4/q6.png'
import question7Png from '@/assets/images/4/q7.png'
import question8Png from '@/assets/images/4/q8.png'
import question9Png from '@/assets/images/4/q9.png'
import question10Png from '@/assets/images/4/q10.png'
import question11Png from '@/assets/images/4/q11.png'
import question12Png from '@/assets/images/4/q12.png'
import question13Png from '@/assets/images/4/q13.png'
import question14Png from '@/assets/images/4/q14.png'
import question15Png from '@/assets/images/4/q15.png'
import picPng from '@/assets/images/4/pic.png'
import videoPng from '@/assets/images/4/video.png'
import audioPng from '@/assets/images/4/audio.png'

// 问题类型对应的图片
const typesMap = {
    1: picPng,
    2: videoPng,
    3: audioPng,
}

const questionImages = {
    1: question1Png,
    2: question2Png,
    3: question3Png,
    4: question4Png,
    5: question5Png,
    6: question6Png,
    7: question7Png,
    8: question8Png,
    9: question9Png,
    10: question10Png,
    11: question11Png,
    12: question12Png,
    13: question13Png,
    14: question14Png,
    15: question15Png,
}

let apiBaseUrl = import.meta.env.VITE_API_BASE_URL

const props = defineProps({
    isVisible: {
        type: Boolean,
        default: false
    },
    types: {
        type: String,
        default: ''
    }
})
const modalStyle = ref()
const modalStyleNum = ref({
    width: '1700px',
    minWidth: '1700px',
    height: '1000px'
})  
const modalStyleAns = ref({
    width: '1700px',
    minWidth: '1700px',
    height: '1000px'
})

const backgroundImage = ref(modal4Png)
//记录用户选择的答案，并且记录对错 option: '', isCorrect: false
const selectedAnswer = ref([])

const emit = defineEmits(['close'])

const currentIndex = ref(0)
// 问题列表,生成 5 个问题
const questionList = ref([])

// 使用计算属性来获取当前问题，这样会随着 currentIndex 的变化而自动更新
const currentQuestion = ref({})

// 问题列表
const getQuestions = async () => {
    const res = await api.question.getQuestions(props.types)
    if (res.code === 0) {
        let data = res.data || []
        questionList.value.length = 0
        questionList.value = data.map(v => ({
            question: v.title,
            options: {
                A: v.region,
                B: v.organ,
                C: v.garden,
                D: v.supervisor,
            },
            answer: v.type,
            remark: v.remark,
            score: v.location,
            paths: v.paths,
            kmlfile: v.kmlfile,
            types: v.types,
        }))
        currentIndex.value = 0
        currentQuestion.value = questionList.value[currentIndex.value] || {}
    }
}
// 监听弹窗是否显示，当弹窗显示时获取问题列表
watch(() => props.isVisible, (newVal) => {
    if (newVal) {
        getQuestions()
        selectedAnswer.value = []
        // 初始化modalStyle为问题样式
        modalStyle.value = modalStyleNum.value
    }
})

// 点击图片放大
const handleClickImage = () => {
    if (currentQuestion.value.types == 1 && currentQuestion.value.kmlfile) {
        window.open(`${apiBaseUrl}${currentQuestion.value.kmlfile}`)
    }
}


// 选中的选项
const selectedOption = ref('')

// 选择选项的方法
const selectOption = (option) => {
    selectedOption.value = option
}

const closeModal = () => {
    emit('close')
}

//显示答案解析页面
const isShowAnswer = ref(false)

const handleConfirm = () => {
    // 检查是否选择了选项
    if (!selectedOption.value) {
        return
    }

    // 检查答案是否正确
    const isCorrect = selectedOption.value === currentQuestion.value.answer
    selectedAnswer.value.push({
        question: currentQuestion.value.question,
        option: selectedOption.value,
        isCorrect,
        score: currentQuestion.value.score,
    })
    // 清空选中的选项
    backgroundImage.value = isCorrect ? correctPng : wrongPng
    modalStyle.value = modalStyleAns.value
    console.log(isCorrect, selectedAnswer.value, selectedOption.value, currentQuestion.value.answer)

    isShowAnswer.value = true
}

//继续下一个问题
const continueNextQuestion = () => {
    // 移动到下一个问题
    currentIndex.value++
    isShowAnswer.value = false
    // 清空选中的选项
    selectedOption.value = ''
    backgroundImage.value = modal4Png
    modalStyle.value = modalStyleNum.value
    currentQuestion.value = questionList.value[currentIndex.value] || {}

    // 如果已经是最后一个问题，关闭弹窗
    if (currentIndex.value >= questionList.value.length) {
        closeModal()
        emit('complete', selectedAnswer.value)
    }
}

</script>

<style lang="scss" scoped>
// 输入框区域
.question-area {
    position: absolute;
    top: 49%;
    left: 50%;
    transform: translate(-50%, -50%);
    width: 1400px;
    height: 640px;

    .question-title {
        font-size: 24px;
        font-weight: bold;
        line-height: 46px;
        letter-spacing: 0em;
        font-variation-settings: "opsz" auto;
        font-feature-settings: "kern" on;
        color: #3D3D3D;
        width: 1000px;
        margin-top: 20px;
        margin-bottom: 20px;
        text-align: left;
    }
}

.common-text {
    color: #3D3D3D;
    font-size: 24px;
    font-weight: bold;
    line-height: 46px;
}

// 选项容器
.options-container {
    width: 100%;
    max-width: 1040px;
}

.options-row {
    display: flex;
    flex-wrap: wrap;
    justify-content: space-between;
    margin-bottom: 20px;
    gap: 20px;
}

.option-item {
    flex: 0 0 calc(50% - 10px);
    display: flex;
    align-items: center;
    padding: 2px 20px;
    border-radius: 6px;
    cursor: pointer;
    transition: all 0.3s ease;
    min-height: 54px;

    &:hover {
        background: rgba(212, 165, 116, 0.15);
        border-color: #F79D00;
        box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
    }

    &.selected {
        background: rgba(212, 165, 116, 0.2);
        border-color: #F79D00;
        box-shadow: 0 0 0 2px rgba(212, 165, 116, 0.3);

        .option-checkbox {
            background: #F79D00;
            border-color: #F79D00;

            &::after {
                opacity: 1;
                transform: rotate(45deg) scale(1);
            }
        }
    }
}

.option-checkbox {
    width: 24px;
    height: 24px;
    border: 1px solid #000000;
    border-radius: 2px;
    margin-right: 15px;
    position: relative;
    transition: all 0.3s ease;
    flex-shrink: 0;
    background: #fff;

    &::after {
        content: '';
        position: absolute;
        top: 3px;
        left: 7px;
        width: 8px;
        height: 12px;
        border: solid white;
        border-width: 0 2px 2px 0;
        opacity: 0;
        transform: rotate(45deg) scale(0.8);
        transition: all 0.2s ease;
    }
}

.option-text {
    font-size: 24px;
    font-weight: bold;
    color: #3D3D3D;
    line-height: 1.4;
    flex: 1;
}

.nickname-input {
    width: 100%;
    height: 72px;
    border: none;
    border-radius: 36px;
    font-size: 32px;
    text-align: center;
    outline: none;
    background: transparent;
    transition: all 0.3s ease;
    color: #4C0000;
    font-weight: 600;

    &:focus {
        background-color: transparent;
    }

    &::placeholder {
        color: #d8d8d8;
        font-size: 32px;
        font-weight: 600;
    }
}

// 登录按钮
.confirm-btn {
    position: absolute;
    bottom: 60px;
    left: 50%;
    transform: translateX(-50%);
    width: 100%;
    height: 96px;
    background: url('../assets/images/4/confirm.png') no-repeat center center;
    background-size: contain;
    cursor: pointer;
    transition: all 0.3s ease;

    &:hover {
        background-image: url('../assets/images/4/confirm_active.png');
        transform: translateX(-50%) scale(1.05);
    }

    &:active {
        transform: translateX(-50%) scale(0.95);
    }
}

.next-btn {
    position: absolute;
    bottom: 90px;
    left: 50%;
    transform: translateX(-50%);
    width: 100%;
    height: 96px;
    background: url('../assets/images/4/continue.png') no-repeat center center;
    background-size: contain;
    cursor: pointer;
    transition: all 0.3s ease;

    &:hover {
        background-image: url('../assets/images/4/continue_active.png');
        transform: translateX(-50%) scale(1.05);
    }

    &:active {
        transform: translateX(-50%) scale(0.95);
    }
}


// 题目编号按钮
.number-btn {
    position: absolute;
    top: 50px;
    left: 50%;
    transform: translateX(-50%);
    width: 400px;
    height: 100px;
    background-size: contain;
    background-repeat: no-repeat;
    background-position: center center;
}

// 问题类型按钮
.types-btn {
    position: absolute;
    top: 80px;
    left: 50%;
    transform: translateX(-50%);
    width: 600px;
    height: 200px;
    background-size: contain;
    background-repeat: no-repeat;
    background-position: center center;
}
</style>